package com.wlf.Controller;

import com.wlf.Entity.Result;
import com.wlf.Entity.SalesForecastTable;
import com.wlf.Service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    //获取数据库SalesForecastTable表中数据
    @GetMapping("/getAll")
    public Result<List<SalesForecastTable>> getAll(){

        List<SalesForecastTable> salesForecastTableList = excelService.getAll();
        return Result.success(salesForecastTableList);
    }
    //导出Excel（Number大于0）
    @GetMapping("/exportByNumberB")
    public void excelExport(HttpServletResponse response) throws IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=salesPredict.xlsx");
        excelService.exportExcel(response);
    }
    //导入Excel
    @PostMapping("/import")
    public void importExcel(@RequestParam("file") MultipartFile file){
        //每次导入删除MSalesForecastTable表中所有数据
        excelService.deleteInfo();
        //导入数据
        excelService.importExcel(file);
    }

}
